package FinancePersonalPackage;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

import javax.swing.table.AbstractTableModel;

public class ModeloRelatorios extends AbstractTableModel {
	
	private ResultSet resultados;
	
	public void atualizaDados() {
		
		try {
			Statement statement = ConexaoDB.getConexao().createStatement();
			
			String query = "SELECT ca.descricao, la.tipo, SUM(la.valor)"+
							"FROM lancamentos la RIGHT JOIN categorias ca "+
							"ON ca.id_categorias = la.id_categoria "+
							"GROUP BY ca.descricao;";
			
			resultados = statement.executeQuery(query);
		
		}
		catch (SQLException e) {
			System.out.println("Erro: " + e.getMessage());
		}
	}
	
	@Override
	public int getColumnCount() {
		return 3;
	}

	@Override
	public int getRowCount() {
		try {
			Statement statement = ConexaoDB.getConexao().createStatement();
			
			ResultSet resultados = statement.executeQuery("SELECT COUNT(*) FROM categorias");

			resultados.first();
			
			return resultados.getInt(1);
		
		} catch (SQLException e) {
			e.printStackTrace();
		}
		
		return 0;
	}
	@Override
	public Object getValueAt(int rowIndex, int columnIndex) {
		
		String r = "";
		
		try {
			resultados.absolute(rowIndex + 1);
			r = resultados.getString(columnIndex + 1);
		}
		catch (SQLException e) {
			e.printStackTrace();
		}
		
		return r;
	}
	
	@Override
	public String getColumnName(int columnIndex) {
		
		switch (columnIndex) {
		case 0:
			return "Categoria";
		case 1:
			return "Tipo";
		case 2:
			return "Valor";
		}
		
		return "";
		
	}

}

